

class RepairProductController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        def resultList = RepairProduct.findByFlag(false)
        [ repairProductInstanceList: resultList, repairProductInstanceTotal: RepairProduct.count() ]
    }

    def show = {
        def repairProductInstance = RepairProduct.get( params.id )

        if(!repairProductInstance) {
            flash.message = "RepairProduct not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ repairProductInstance : repairProductInstance ] }
        complete()
    }

    def delete = {
        def repairProductInstance = RepairProduct.get( params.id )
        if(repairProductInstance) {
            try {
                repairProductInstance.delete()
                flash.message = "RepairProduct ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "RepairProduct ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "RepairProduct not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def repairProductInstance = RepairProduct.get( params.id )

        if(!repairProductInstance) {
            flash.message = "RepairProduct not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ repairProductInstance : repairProductInstance ]
        }
    }

    def update = {
        def repairProductInstance = RepairProduct.get( params.id )
        if(repairProductInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(repairProductInstance.version > version) {
                    
                    repairProductInstance.errors.rejectValue("version", "repairProduct.optimistic.locking.failure", "Another user has updated this RepairProduct while you were editing.")
                    render(view:'edit',model:[repairProductInstance:repairProductInstance])
                    return
                }
            }
            repairProductInstance.properties = params
            if(!repairProductInstance.hasErrors() && repairProductInstance.save()) {
                flash.message = "RepairProduct ${params.id} updated"
                redirect(action:show,id:repairProductInstance.id)
            }
            else {
                render(view:'edit',model:[repairProductInstance:repairProductInstance])
            }
        }
        else {
            flash.message = "RepairProduct not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def repairProductInstance = new RepairProduct()
        repairProductInstance.properties = params
        return ['repairProductInstance':repairProductInstance]
    }

    def save = {
        def exportProduct = ExportProduct.findBySellNumber(params.number)
        if (exportProduct == null)
        {
            flash.message = "对不起，没有该销售单，请重新输入！！"
            redirect(action:"create", params:[productName:params.productName,amount:params.amount,modelNo:params.modelNo])
            return
        }
        def repairProductInstance = new RepairProduct(params)
        if(!repairProductInstance.hasErrors() && repairProductInstance.save()) {
            repairProductInstance.setRepairNumber(repairProductInstance.getRepairNumber()+1)
            flash.message = "RepairProduct ${repairProductInstance.id} created"
            redirect(action:show,id:repairProductInstance.id)
        }
        else {
            render(view:'create',model:[repairProductInstance:repairProductInstance])
        }
    }

    def startFlow = {
        def id = params.id
        start{
            on("success").to "endRepair"
            on("failed").to "endFailed"
        }
        endRepair{
            def repairProduct = RepairProduct.findById(id)
            repairProduct.setFlag(true)
            repairProduct.save()
            return
        }
 
        endFailed {
            def repairProduct = RepairProduct.findById(id)
            def rg = new ReturnedGoods()

            def productName = repairProduct.getProductName()
            def modelNo = repairProduct.getModelNo()
            def amount = repairProduct.getRepairNumber()
            def time = repairProduct.getTime()
            def reason = repairProduct.getReason()
            def number = repairProduct.getNumber()

            // 创建退货单
            rg.setProductName(productName)
            rg.setModelNo(modelNo)
            rg.setAmount(amount)
            rg.setTime(time)
            rg.setReason(reason)
            rg.setNumber(number)
            rg.save()

            def exportProduct = ExportProduct.findBySellNumber(number)

            if (amount == exportProduct.getAmount())
            {
                def amount1 = exportProduct.getAmount()
                def outPrice = exportProduct.getOutPrice()
                def inPrice = exportProduct.getInPrice()
                def sellProductId = exportProduct.getId()
                println sellProductId
                def pureProfits = (outPrice - inPrice)*amount1
                def profit = Profit.findByPureProfitsAndSellProductId(pureProfits,sellProductId)
                profit.delete()//删除利润
                exportProduct.delete()
            }
            if(amount < exportProduct.getAmount())
            {
                def outPrice = exportProduct.getOutPrice()
                def inPrice = exportProduct.getInPrice()
                def sellProductId = exportProduct.getId()
                def pureProfits = (outPrice - inPrice)*amount
                def profit = Profit.findBySellProductId(sellProductId)
                profit.setPureProfits(profit.getPureProfits()-pureProfits)
                profit.save()
                exportProduct.setAmount(exportProduct.getAmount()-amount)
                exportProduct.save()
            }
        }

    }
}
